//
//  LeaderController.m
//  PropertyMananger
//
//  Created by Mansion on 17/3/4.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "LeaderController.h"
#import "LeaderFormCell.h"
#import "MonthStasticsCell.h"
#import "LeaderDetailController.h"
#import "MonthStasticsController.h"
#import "PayDetailController.h"
#import "PaymentStasticCell.h"
#import "LeaderModel.h"
#import "SelectedMonthView.h"

#import "LeaderRepairController.h"
#import "LeaderComplainController.h"

@interface LeaderController ()<SelectedMonthViewDelegate>
@property (nonatomic,copy) NSString * timeStr;//系统默认的时间
@property (nonatomic,strong) LeaderFormCell * leaderCell;//领导报表
@property (nonatomic,assign) NSInteger  selectedMonth;//选择月份

@property (nonatomic,strong)  NSDateComponents* comp;//系统的时间

@property (nonatomic,copy) NSString * yearStr;
@property (nonatomic,copy) NSString * selectedTimeStr;

@property (nonatomic,strong) NSMutableArray * leaderArray;

@property (nonatomic,assign) CGFloat currentNum;
@property (nonatomic,assign) CGFloat totalNum;

@property (nonatomic,strong) SelectedMonthView * selectedView;

@end

@implementation LeaderController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self getYearAndMonthData];
    
    [self getYearData];
    
    self.leaderArray = [[NSMutableArray alloc]init];
    
    [self initTitleViewName:@"领导报表"];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 0, BHscreenW, BHscreenH-64) isGroup:NO];
    
    self.yearStr = self.selectedTimeStr;
    
    [self postLeaderFromData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       
        if (indexPath.row == 0) {
            
            LeaderFormCell * cell = [LeaderFormCell cellWithTableView:tableView];
            if (self.leaderArray.count != 0) {
                
                LeaderModel * model = self.leaderArray[0];
            
                cell.model = model;
                
                DLog(@"投诉 ==%@  ",model.okComplainall);
                
            }
            
            [cell.monthButton addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.leftButton addTarget:self action:@selector(IndexClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.rightButton addTarget:self action:@selector(IndexClick:) forControlEvents:UIControlEventTouchUpInside];
            
            cell.leftButton.tag = 400;
            cell.rightButton.tag = 410;
            
            cell.monthButton.tag = 100;
            cell.yearLabel.text = self.yearStr;
            
            return cell;
        }
    }else if (indexPath.section == 1){
    
        if (indexPath.row == 0) {
        
            MonthStasticsCell * cell = [MonthStasticsCell cellWithTableView:tableView];
            
            [cell.detailBtn addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.detailBtn.tag = 200;
            
            if (self.leaderArray.count != 0) {
                
               LeaderModel * model = self.leaderArray[0];
                
                cell.model = model;
                
                DLog(@"model.monthModel.month3Count == %@",model.monthModel.month3Count);
}

            return cell;
        }

    }else if (indexPath.section == 2){
    
        if (indexPath.row == 0) {
            
            PaymentStasticCell * cell = [PaymentStasticCell cellWithTableView:tableView];
            if (self.leaderArray.count != 0) {
                
                LeaderModel * model = self.leaderArray[0];
                cell.model = model;
                
            }

            
            [cell.monthButton addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            cell.monthButton.tag = 300;
            
            [cell.leftButton addTarget:self action:@selector(IndexClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.rightButton addTarget:self action:@selector(IndexClick:) forControlEvents:UIControlEventTouchUpInside];
            
            cell.leftButton.tag = 420;
            cell.rightButton.tag = 430;
            
            cell.yearLabel.text = self.yearStr;
            return cell;
        }
    
    }
    
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 270;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        
        return 0.001f;
    }
    return 15;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        
//        LeaderDetailController * detailVC = [[LeaderDetailController alloc]init];
//        detailVC.selectedTime = self.yearStr;
//        [self.navigationController pushViewController:detailVC animated:YES];
        
    }else if (indexPath.section == 1){
        
//        MonthStasticsController * detailVC = [[MonthStasticsController alloc]init];
//       
//        [self.navigationController pushViewController:detailVC animated:YES];
    
    }else if (indexPath.section == 2){
        
//        PayDetailController * detailVC = [[PayDetailController alloc]init];
//        detailVC.selectedTime = self.yearStr;
//        
//        [self.navigationController pushViewController:detailVC animated:YES];
    }

}
- (void)IndexClick:(UIButton *)button{

    if (button.tag == 400) {
        
        LeaderDetailController * detailVC = [[LeaderDetailController alloc]init];
        detailVC.selectedTime = self.yearStr;
        detailVC.index = 0;
        [self.navigationController pushViewController:detailVC animated:YES];

        
        DLog(@"点击报修单详情");
    }else if(button.tag == 410){
    
        LeaderDetailController * detailVC = [[LeaderDetailController alloc]init];
        detailVC.selectedTime = self.yearStr;
        detailVC.index = 1;
        [self.navigationController pushViewController:detailVC animated:YES];

         DLog(@"点击投诉单详情");
    }else if(button.tag == 420){
        
        PayDetailController * detailVC = [[PayDetailController alloc]init];
        detailVC.selectedTime = self.yearStr;
        detailVC.index = 0;
        [self.navigationController pushViewController:detailVC animated:YES];
        
        DLog(@"点击投诉单详情");
    }else if(button.tag == 430){
        
        PayDetailController * detailVC = [[PayDetailController alloc]init];
        detailVC.selectedTime = self.yearStr;
        detailVC.index = 1;
        [self.navigationController pushViewController:detailVC animated:YES];
        
        DLog(@"点击投诉单详情");
    }

}
- (void)detailBtnClick:(UIButton *)button{

    if (button.tag == 100) {
        DLog(@"点击选择月份");
     //时间选择样式一
//        __block LeaderController *weakSelf = self;
//        dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerYearMonthMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
//        [self.view addSubview:dateTimePickerView];
//        
//        dateTimePickerView.topViewColor = BHGrayColor;
//        dateTimePickerView.buttonTitleColor = Theme_Color;
//        
//        dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
//            NSLog(@"%@", datetimeStr);
//            //  weakSelf.tableHeaderView.yearLabel.text = [NSString stringWithFormat:@"%@",datetimeStr];
//            
//            NSArray * monthArray = [datetimeStr componentsSeparatedByString:@"-"];
//            DLog(@"月份 == %@",monthArray[1]);
//            
//            if ([monthArray[1]integerValue] <=9) {
//                
//                weakSelf.yearStr = [NSString stringWithFormat:@"%@-0%@",monthArray[0],monthArray[1]];
//                weakSelf.timeStr = [NSString stringWithFormat:@"%@年/0%@月",monthArray[0],monthArray[1]];
//                
//            }else{
//                
//                weakSelf.yearStr = [NSString stringWithFormat:@"%@-%@",monthArray[0],monthArray[1]];
//                
//                weakSelf.timeStr = [NSString stringWithFormat:@"%@年/0%@月",monthArray[0],monthArray[1]];
//            }
//            
//            NSDictionary * dict = @{@"key":weakSelf.yearStr};
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"LeaderSelectedTimeNotification" object:nil userInfo:dict];
//            
//            
//           [weakSelf postLeaderFromData];
//            
//             [weakSelf.tableView reloadData];
//        };
//        
//        if (dateTimePickerView) {
//            [self.view addSubview:dateTimePickerView];
//            [dateTimePickerView showHcdDateTimePicker];
//        }
       //时间选择样式二
            self.selectedView = [[SelectedMonthView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 270)/2, self.view.frame.size.height/2-80, 270, 210)];
            [self.view addSubview:self.selectedView];
        
            self.selectedView.delegate = self;
        
            [self.selectedView selectedMonthView];
        
        
    }else if (button.tag == 200){

        MonthStasticsController * detailVC = [[MonthStasticsController alloc]init];
        [self.navigationController pushViewController:detailVC animated:YES];
       
    }else if (button.tag == 300){
        DLog(@"点击选择月份");
        
//        __block LeaderController *weakSelf = self;
//        dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerYearMonthMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
//        [self.view addSubview:dateTimePickerView];
//        
//        dateTimePickerView.topViewColor = BHGrayColor;
//        dateTimePickerView.buttonTitleColor = Theme_Color;
//        
//        dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
//            NSLog(@"%@", datetimeStr);
//            //  weakSelf.tableHeaderView.yearLabel.text = [NSString stringWithFormat:@"%@",datetimeStr];
//            
//            NSArray * monthArray = [datetimeStr componentsSeparatedByString:@"-"];
//            DLog(@"月份 == %@",monthArray[1]);
//            
//            if ([monthArray[1]integerValue] <=9) {
//                
//                weakSelf.yearStr = [NSString stringWithFormat:@"%@-0%@",monthArray[0],monthArray[1]];
//                weakSelf.timeStr = [NSString stringWithFormat:@"%@-0%@",monthArray[0],monthArray[1]];
//                
//            }else{
//                
//                weakSelf.yearStr = [NSString stringWithFormat:@"%@-%@",monthArray[0],monthArray[1]];
//                
//                weakSelf.timeStr = [NSString stringWithFormat:@"%@-%@",monthArray[0],monthArray[1]];
//            }
//            
//            [weakSelf.tableView reloadData];
//
//            [weakSelf postLeaderFromData];
//        };
//        
//        if (dateTimePickerView) {
//            [self.view addSubview:dateTimePickerView];
//            [dateTimePickerView showHcdDateTimePicker];
//        }
        
        //时间选择样式二
        self.selectedView = [[SelectedMonthView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 270)/2, self.view.frame.size.height/2-80, 270, 210)];
        [self.view addSubview:self.selectedView];
        
        self.selectedView.delegate = self;
        
        [self.selectedView selectedMonthView];
     
    }

}
- (void)didSelectMonth:(NSInteger)num{
    NSLog(@"点击了 == %ld",(long)num);
    //   self.tableHeaderView.yearLabel.text = [NSString stringWithFormat:@"%ld年/%ld月份",self.comp.year,num + 1];
  self.selectedMonth = [[NSString stringWithFormat:@"0%ld",num+1]integerValue];
    
    DLog(@"====%ld",self.selectedMonth);
    //    if (num == 12) {
    //
    //         self.tableHeaderView.yearLabel.text = [NSString stringWithFormat:@"2017年/全年"];
    //    }else{
    //    self.tableHeaderView.yearLabel.text = [NSString stringWithFormat:@"2017年/%ld月份",num + 1];
    //    }
    if (num <9) {
        
        self.yearStr = [NSString stringWithFormat:@"%ld-0%ld",self.comp.year,self.selectedMonth];
    }else{
    
    self.yearStr = [NSString stringWithFormat:@"%ld-%ld",self.comp.year,self.selectedMonth];
    }
    
    DLog(@"==%@",self.yearStr);
       
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSString stringWithFormat:@"%ld",self.selectedMonth] forKey:@"month"];
    [userDefaults synchronize];
    
    
    
    [self.tableView reloadData];
    
   [self postLeaderFromData];
    
    
}

#pragma mark - 领导报表数据请求
- (void)postLeaderFromData{
    
    NSDictionary * parms = @{
                             @"Cid":userModel.CommunityID,
                             @"MonthTime":self.yearStr,
                             @"json":@"\{\"jsonEntity\":\{},\"action\":\"getallreport\",\"version\":\"V1.0\"}"};
    DLog(@"领导提交的参数===%@",parms);
    
    [HomeNetWorkRequest postLeaderFromSuccess:^(id data) {
        
        [self.leaderArray removeAllObjects];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * dict1 = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"领导报表数据 == %@",dict1);
        
        DLog(@"%@",dict[@"Msg"]);
        
        LeaderModel * model = [[LeaderModel alloc]init];
        [model setValuesForKeysWithDictionary:dict1];
        
        DLog(@"%@",model.Complainall);
        [self.leaderArray addObject:model];
        
        
         //方法一：直接把里面的model赋给他,方法二在LeaderModel里
//        MonthModel * monthModel = [[MonthModel alloc]init];
//      
//        [monthModel setValuesForKeysWithDictionary:dict1[@"MonthReportdata"]];
//        
//        [self.leaderArray addObject:monthModel];
//       
//        model.monthModel = monthModel;
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
    
}
#pragma mark - 获取系统时间
- (void)getYearAndMonthData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
    
    NSArray * timeArr = [timeDataStr componentsSeparatedByString:@"-"];
    NSString * timeStr = [NSString stringWithFormat:@"%@-%@",timeArr[0],timeArr[1]];
    DLog(@"拼接好的时间===%@",timeStr);
    self.selectedTimeStr = timeStr;
}
- (void)getYearData{
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate * dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    self.comp = [gregorian components: unitFlags  fromDate:dt];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    
    
}

@end
